package com.app.vhtable;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.PromptManager;
import com.app.vhtable.vhtableview.VHBaseAdapter;

import java.util.ArrayList;
import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.FieldPlace;
import phone.qyg.com.qyg.model.FieldPrice;


/**
 * Created by jian on 2016/7/20.
 *
 * 都实现VHBaseAdapter接口
 */
public class VHTableAdapter implements VHBaseAdapter {
    private Context context;
    private ArrayList<FieldPlace> titleData;
    private ArrayList<ArrayList<FieldPrice>> dataList;
    private List<FieldPrice> tempFields;
    private OnVHTabletemOnClickUpdate onVHTabletemOnClickUpdate;

    public VHTableAdapter(Context context, ArrayList<FieldPlace> titleData, ArrayList<ArrayList<FieldPrice>> dataList, OnVHTabletemOnClickUpdate onVHTabletemOnClickUpdate ) {
        this.context=context;
        this.titleData=titleData;
        this.dataList=dataList;
        this.onVHTabletemOnClickUpdate=onVHTabletemOnClickUpdate;
        tempFields = new ArrayList(ApplicationConfig.FIELD_NUMBER);
    }

    //表格内容的行数，不包括标题行
    @Override
    public int getContentRows() {
        return dataList.size();
    }

    //列数
    @Override
    public int getContentColumn() {
        return titleData.size();
    }

    //标题的view，这里从0开始，这里要注意，一定要有view返回去，不能为null，每一行
    // 各列的宽度就等于标题行的列的宽度，且边框的话，自己在这里和下文的表格单元格view里面设置
    @Override
    public View getTitleView(int columnPosition, ViewGroup parent) {

        TextView tv_item = new TextView(context);
//        tv_item.setBackgroundResource(R.drawable.bg_shape_gray);
        if(0==columnPosition){
//            tv_item.setPadding(50, 40, 50, 40);  7月6号 是这个状态
            //TODO 重点控制第一列的数据展示
            tv_item.setPadding(CommonUtil.dip2px(QXApplication.getContext(),20),
                    CommonUtil.dip2px(QXApplication.getContext(),5),
                    CommonUtil.dip2px(QXApplication.getContext(),20),
                    CommonUtil.dip2px(QXApplication.getContext(),5));
            //setPadding(left, top, right, bottom)
            tv_item.setText("heheh");
        }else {
            tv_item.setPadding(0,
                            CommonUtil.dip2px(QXApplication.getContext(),5),
                            0,
                            CommonUtil.dip2px(QXApplication.getContext(),5));
//            tv_item.setPadding(10, 10, 10, 10);
        }
//        ViewGroup.LayoutParams params = tv_item.getLayoutParams();
        //考虑标题文字太少 设置最小值
        tv_item.setMinWidth(CommonUtil.dip2px(QXApplication.getContext(),50));
        FieldPlace fieldPlace = titleData.get(columnPosition);
//        tv_item.setText(fieldPlace.week+"\n"+fieldPlace.date);
        tv_item.setText(CommonUtil.setMemSpannAddCout(fieldPlace.week,fieldPlace.date));
        tv_item.setTextColor(context.getResources().getColor(R.color.shallow_black));
        tv_item.setGravity(Gravity.CENTER);
        return tv_item;
    }


    //表格正文的view，行和列都从0开始，宽度的话在载入的时候，默认会是以标题行各列的宽度，高度的话自适应
    @Override
    public View getTableCellView(final int contentRow, final int contentColum, View view, ViewGroup parent) {
        if(null==view) {
            view = new TextView(context);
        }

        final FieldPrice fieldPrice = dataList.get(contentRow).get(contentColum);
        if(contentColum == 0){
            view.setBackgroundResource(R.color.no_color);
            ((TextView)view).setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
            //TODO 大小关键代码  7月6号是 view.setPadding(0, 30, 0, 10);
            view.setPadding(0, CommonUtil.dip2px(context,15), CommonUtil.dip2px(context,10), 0);//int left, int top, int right, int bottom

//            view.setPadding(10, 30, 0, 10);//int left, int top, int right, int bottom
            ((TextView)view).setText(fieldPrice.time_str);
            ((TextView)view).setTextColor(context.getResources().getColor(R.color.shallow_black));
        }else{
//            view.setBackgroundResource((fieldPrice.status == 0)?R.drawable.blue_vh_frame:R.drawable.ash_vh_frame);
            int backColorRes = R.drawable.venue_yes_reserve;
            int status = fieldPrice.status;
            if(status == 0){
                ((TextView)view).setText("");
                backColorRes = R.drawable.venue_yes_reserve;
            }else if(fieldPrice.status == 1){
                ((TextView)view).setText("");
                backColorRes = R.drawable.venue_no_reserve;
            }else if(fieldPrice.status == 2){
                backColorRes = R.drawable.venue_select_reserve;
            }
            view.setBackgroundResource(backColorRes);
            ((TextView)view).setGravity(Gravity.CENTER);
            ((TextView)view).setTextColor(QXApplication.getContext().getResources().getColor(R.color.white));
//            ((TextView)view).setTextSize(TypedValue.COMPLEX_UNIT_DIP,11);
            view.setPadding(0, 5, 0, 5);
//            ((TextView)view).setText(fieldPrice.price+"");
        }


//        ((TextView)view).setGravity(Gravity.CENTER);
        //为了更灵活一些，在VHTableView没收做设置边框，在这里通过背景实现，我这里的背景边框是顺手设的，要是想美观点的话，对应的边框做一下对应的设置就好
        //TODO  禁止调
//        ((TextView)view).setTextColor(context.getResources().getColor(R.color.white));
        final View temp = view;
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("-----contentRow---->"+contentRow+",contentColum"+contentColum);
                if(contentColum == 0)
                    return;
                if (fieldPrice.status == 0){
                    //添加预定场地信息
                    if(tempFields.size() >= ApplicationConfig.FIELD_NUMBER){
                        PromptManager.showToast(R.string.current_file_number_hint);
                        return;
                    }
//                    if(CommonUtil.isRequestList(tempFields)){
//                        int freeCount = 0;
//                        for(int i=0,j=tempFields.size();i<j;i++){
//                            FieldPrice itemFieldPrice = tempFields.get(i);
//                            if(itemFieldPrice.price<=0){
//                                freeCount++;
//                            }
//                        }
//
//                        if(freeCount>=2){
//                            PromptManager.showToast(R.string.current_free_number_hint);
//                            return;
//                        }
//                    }
                    tempFields.add(fieldPrice);
                    temp.setBackgroundResource(R.drawable.venue_select_reserve);
                    ((TextView)temp).setText(QXApplication.getContext().getString(R.string.reserve_venue));
                    fieldPrice.status = 2;
                    if(onVHTabletemOnClickUpdate != null)
                        onVHTabletemOnClickUpdate.OnClickReserves(tempFields);

                }else if(fieldPrice.status == 2){
                    ((TextView)temp).setText("");
                    temp.setBackgroundResource(R.drawable.venue_yes_reserve);
                    fieldPrice.status = 0;
                    //删除预定场地信息
                    tempFields.remove(fieldPrice);
                    if(onVHTabletemOnClickUpdate != null)
                        onVHTabletemOnClickUpdate.OnClickReserves(tempFields);
                }

            }
        });
        return view;
    }


    @Override
    public Object getItem(int contentRow) {
        return dataList.get(contentRow);
    }


    //每一行被点击的时候的回调
    @Override
    public void OnClickContentRowItem(int row, View convertView) {
    }


    public interface OnVHTabletemOnClickUpdate{
        public void OnClickReserves(List<FieldPrice> prices);
    }
}
